package com.sitech.ibnms.c3p.task;

import com.sitech.ibnms.c3p.task.notice.ExecutePolicy;
import com.sitech.ibnms.c3p.util.C3pConstants;
import com.sitech.ismp.messageObject.cc.C3pTaskParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jms.*;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;

/**
 * 短信上行调用任务
 * Created by liujhc on 11/6.
 */
@Component
public class TaskStartupMessageListener implements MessageListener {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private C3pTaskFlowServiceExtends taskFlowServiceExtends;

    @Override
    public void onMessage(Message message) {
        try {
            logger.info("Received a message: {}", message instanceof TextMessage ? ((TextMessage) message).getText() : message);
            if (message instanceof ObjectMessage) {
                Serializable object = ((ObjectMessage) message).getObject();
                if (object instanceof C3pTaskParam) {
                    C3pTaskParam param = (C3pTaskParam) object;
                    HashMap<String, String> context = new HashMap<String, String>();
                    if (param.isMailNotify()) {
                        context.put(C3pConstants.CONTEXT_MAIL_NOTIFY, "true");
                    }
                    if (param.isSmsNotify()) {
                        context.put(C3pConstants.CONTEXT_SMS_NOTIFY, "false");
                    }
                    context.put(C3pConstants.CONTEXT_SCHEDULE_ID, "SMS_UPLINK");
                    taskFlowServiceExtends.startTask(param.getC3pTaskId(), param.getUserId(), param.getTitle(), Collections.<Integer, ExecutePolicy>emptyMap(), context);
                }
            }
        } catch (JMSException e) {
            logger.error(e.getMessage(), e);
        }
    }
}
